package com.jnu.myaccount;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jnu.myaccount.adapter.ItemAdapter;
import com.jnu.myaccount.data.GridItems;
import com.jnu.myaccount.record.IncomeFragment;
import com.jnu.myaccount.record.PaidFragment;
import com.jnu.myaccount.adapter.RecordPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{

    TabLayout tabLayout;
    ViewPager viewPager;
    ImageView back;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        //1.查找控件
        tabLayout = findViewById(R.id.record_tab);
        viewPager = findViewById(R.id.record_vp);
        back=findViewById(R.id.record_lv_back);
        back.setOnClickListener(this);

        //2.设置ViewPager加载页面
        initPager();


    }


    private void initPager() {
//        初始化ViewPager页面的集合
        List<Fragment> fragmentList = new ArrayList<>();
//        创建收入和支出页面，放置在Fragment当中
        PaidFragment outFrag = new PaidFragment(); //支出
        IncomeFragment inFrag = new IncomeFragment(); //收入
        fragmentList.add(outFrag);
        fragmentList.add(inFrag);

//        创建适配器
        RecordPagerAdapter pagerAdapter = new RecordPagerAdapter(getSupportFragmentManager(), fragmentList);
//        设置适配器
        viewPager.setAdapter(pagerAdapter);
        //将TabLayout和ViwePager进行关联
        Bundle bundle = getIntent().getExtras();   //得到传过来的bundle
        if(bundle!=null) {
            String type = bundle.getString("type");//读出数据
            if(type.equals("Income"))
                viewPager.setCurrentItem(1);//这条方法设置viewPage的默认页面
            else
                viewPager.setCurrentItem(0);//这条方法设置viewPage的默认页面
        }

        tabLayout.setupWithViewPager(viewPager);
    }

    /* 点击事件*/
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.record_lv_back://back-ONCLICK
                finish();
                break;
        }
    }
}